package org.cwnuit.train.acton;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.cwnuit.train.domain.RuleStime;
import org.cwnuit.train.domain.Rules;
import org.cwnuit.train.domain.Stime;
import org.cwnuit.train.domain.Wtype;
import org.cwnuit.train.service.RuleStimeService;
import org.cwnuit.train.service.RulesService;
import org.cwnuit.train.service.StimeService;
import org.cwnuit.train.service.WtypeService;
import org.cwnuit.train.utils.MapUtil;

@SuppressWarnings("all")
public class RuleStimeAction extends HttpServlet{
	private StimeService stimeService = new StimeService();
	private WtypeService wtypeService = new WtypeService();
	private RulesService rulesServices = new RulesService();
	private RuleStimeService rulestimeService = new RuleStimeService();
	
	protected void service(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		
		String cmd = request.getParameter("cmd");
        if (cmd.equals("list")) {
        	request.setAttribute("content_tittle", "本次学习章程管理");
        	int sid = Integer.parseInt(request.getParameter("sid"));
        	Stime stime = stimeService.findStime("SELECT * FROM Stime WHERE sid = " + sid);
        	Wtype wtype = wtypeService.findWtype("SELECT * FROM Wtype WHERE wid = " + stime.getWid());
        	List<Rules> ruleslist = rulesServices.findRules("SELECT * FROM Rules WHERE rlevel = 4 ORDER BY rid ASC");
        	request.setAttribute("stime", stime);
        	request.setAttribute("wtype", wtype);
        	request.setAttribute("rulesdata", new MapUtil().getRulesMap(ruleslist));
        	
        	String SQL = "select * from rules where rid in (select rid from rulestime where sid = "+sid+")";
        	List<Rules> rules = rulesServices.findRules(SQL);
           
        	request.setAttribute("currentdata", new MapUtil().getRulesMap(rules));
			request.getRequestDispatcher("web/rulestime/list.jsp").forward(request, response);
		} else if(cmd.equals("input")){
			request.setAttribute("content_tittle", "本次学习规章编辑");
				if (updateRuleStime(request)) {
					request.setAttribute("msg", "恭喜你，编辑成功！");
				}else {
					request.setAttribute("msg", "对不起，编辑失败！");
				}
				request.getRequestDispatcher("stime?cmd=list").forward(request, response);
		}
		
	}
	
//////////////////////////////////////////////////工具方法////////////////////////////////////////////////	
	private boolean updateRuleStime(HttpServletRequest request) {
		int sid = Integer.parseInt(request.getParameter("sid"));
		String SQL = "DELETE FROM RuleStime WHERE sid = " + sid;
		rulestimeService.deleteRuleStime(SQL);
		String rids[] = request.getParameterValues("rid");
		boolean flag = true;
		if(rids!=null)
		for(String rid:rids){
			String SQL1 = "INSERT INTO RuleStime(rid,sid) VALUES(" +rid+ "," + sid + ")";
			flag = rulestimeService.addRuleStime(SQL1);
			if(!flag) return false;
		}
		return flag;
	}
	
	
}
